# 定义两个类 Question， Exam
class Question:
    # 构造方法，有参构造
    def __init__(self, question_text, options):
        self.question_text = question_text
        self.options = options
        self.user_answer = None

    # display 展示试题与选项
    def display(self):
        print(self.question_text)
        for option in self.options:
            print(option)

    # 获取用户的答案
    def get_user_answer(self):
        while True:
            user_input = input("请输入您的答案（A/B/C/D）：")
            if user_input in ['A', 'B', 'C', 'D']:
                self.user_answer = user_input
                break
            else:
                print('请重新输入:A,B,C,D')

    # 判断答案是否正确
    def is_correct(self, correct_answer):
        return self.user_answer == correct_answer

class Exam:
    # 构造方法，有参构造
    def __init__(self, exam_file, answers_file):
        self.questions = []
        self.answers = []
        self.load_exam(exam_file, answers_file)

    def load_exam(self, exam_file, answers_file):
        with open(exam_file, 'r', encoding='utf-8') as file:
            exam_lines = file.readlines()

        with open(answers_file, 'r', encoding='utf-8') as file:
            self.answers = file.readline().split()

        current_question = ''
        current_options = []

        for line in exam_lines:
            stripped_line = line.strip()
            if stripped_line:
                if stripped_line[0].isdigit() and stripped_line[1] == '.':  # 检查是否为题而不是空行
                    if current_question:  # 如果当前已经有题目了，就添加到列表中
                        self.add_question(current_question, current_options)
                    current_question = stripped_line  # 开始新的题目
                    current_options = []
                else:
                    current_options.append(stripped_line)
            else:   # 遇到空行则结束上一题的选项收集
                if current_question:
                    self.add_question(current_question, current_options)
                    current_question = ''
                    current_options = []

        if current_question:  # 添加最后一道题
            self.add_question(current_question, current_options)

    def add_question(self, question_text, options):
        self.questions.append(Question(question_text, options))

    def start_exam(self):
        score = 0
        total_questions = len(self.questions)

        for i, (question, correct_answer) in enumerate(zip(self.questions, self.answers), 1):
            print(f"问题 {i}/{total_questions}:")
            question.display()
            question.get_user_answer()
            if question.is_correct(correct_answer):
                score += 1
            print('-'*40)

        self.save_results(score, total_questions)

    def save_results(self, score, total_questions):
        with open('exam_results.txt', 'w', encoding='utf-8') as f:
            f.write(f"总分：{score}/{total_questions}\n")
            f.write(f'正确率：{score / total_questions * 100: .2f}%\n')
            f.write("详细结果：\n")
            for i, (question, correct_answer) in enumerate(zip(self.questions, self.answers), 1):
                f.write(f"问题：{question.question_text}\n")
                f.write(f"您的答案是：{question.user_answer}， 正确答案是：{correct_answer}\n")
                f.write('-'*40 + '\n')


exam = Exam('exam.txt', 'answers.txt')
exam.start_exam()





